$(function () {
  // 获取系统时间
  template.defaults.imports.formatDate = function (date) {
    date = new Date(date);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var day = date.getDate();
    return year + "-" + month + "-" + day;
  };

  // ---------------获取评论内容,渲染页面-------------
  function getComments() {
    $.ajax({
      type: "GET",
      url: "http://localhost:8888/admin/comments",
      success: function (res) {
        if (res.status === 0) {
          // 把列表模板的内容添加到tbody中
          var con = template("table-tpl", res);
          $(".layui-table tbody").html(con);
        }
      },
      // headers: {
      //   Authorization:
      //     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTUyNTY4MywiZXhwIjoxNTk5NTI5MjgzfQ.Ya9BvZEm2TlhKHerWrRhslB6KM0Of7-ykKDTneDO9Ew",
      // },
      headers: {
        Authorization: localStorage.getItem('token')
      },
    });
  }
  getComments();
  // ------------------------------删除------------------------------------
  // 获取删除按钮
  $(".layui-card tbody").on("click", ".del", function (e) {
    var id = $(e.target).data("id");
    // 弹层 询问 是否删除
    layer.confirm("你确定要删除吗？", function (index) {
      $.ajax({
        type: "DELETE",
        url: "http://localhost:8888/admin/comments/" + id, //接口+id  一定要注意路径   否则报404
        success: function (res) {
          if (res.status === 0) {
            layer.close(index);
            getComments();
          }
        },
        // headers: {
        //   Authorization:
        //     "Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTU5OTUyNTY4MywiZXhwIjoxNTk5NTI5MjgzfQ.Ya9BvZEm2TlhKHerWrRhslB6KM0Of7-ykKDTneDO9Ew",
        // },
        headers: {
          Authorization: localStorage.getItem('token')
        },
      });
    });
  });
});